package qimao.com.edu.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import qimao.com.edu.dao.UserDao;
import qimao.com.edu.dao.impl.UserDaoImpl;
import qimao.com.edu.entity.Users;
import qimao.com.edu.service.UsersService;

public class UsersServiceImpl implements UsersService {

	UserDao userDao = new UserDaoImpl();

	@Override
	public List<Users> getAllUsers() {
		return userDao.AllUsers();
	}

	// 用户注册
	@Override
	public int registerUser(Users user) {
		// TODO Auto-generated method stub
		if(userDao.selectUserByUsername(user.getUsername())!=null) {
			return -1;
		}
		return userDao.registerUser(user);
	}

	// 根据ID获取用户
	@Override
	public Users getUserById(Integer user_id) {
		return userDao.userById(user_id);
	}

	// 更新用户信息
	@Override
	public int updateUser(Users user) {
		// TODO Auto-generated method stub
		return userDao.updateUser(user);
	}

	// 更新用户状态
	@Override
	public int updateUserStatus(Users user) {
		// TODO Auto-generated method stub
		return userDao.updateUserStatus(user);
	}

	// 删除用户
	@Override
	public int deleteUser(Integer user_id) {
		return userDao.deleteUser(user_id);
	}

	// 重置密码
	@Override
	public int resetPassword(Integer user_id, String newPassword) {
		// TODO Auto-generated method stub
		return userDao.resetPassword(user_id, newPassword);
	}

	public Map<String,Object> userLogin(String username, String password) {
		Map<String,Object> map = new HashMap();
		Users loginUser = userDao.selectUser(username, password);
		
		if(loginUser != null) {
			map.put("loginUser",loginUser);
		}else {
			loginUser = userDao.selectUserByUsername(username);
			if(loginUser == null) {
				map.put("msg","用户名不存在");
			}else {
				map.put("msg","密码错误");
			}
		}
		return map;
	}

}
